<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/15 0015
 * Time: 13:58
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Admin extends Controller
{
    public function adminAdmin()
    {
        return $this->fetch();
    }

    public function adminList()
    {
        $list = Db::name('admin')->order('id desc')->select();
        $this->assign('data',$list);
        return $this->fetch();
    }

    public function adminAdd()
    {
        if (request()->isPost()) {
            $name = Db::name('admin')->where('name', input('name'))->find();
            if ($name) {
                return show(431);
            }
//            $rol = Db::name('admin')->where('role', input('role'))->find();
//            $a = $rol['role'];
//            if ($a == 0) {
//                return show(430);
//            }

            $admin = [
                'name' => input('name'),
                'password' => md5(input('password')),
                'phone' => input('phone'),
                'email' => input('email'),
                'role' => input('role'),
            ];

            $res = Db::name('admin')->insert($admin);
            if ($res) {
                return show(200);
            } else {
                return show(403);
            }
        }
        return $this->fetch();
    }

    public function adminEdit()
    {
        return $this->fetch();
    }

    public function adminRole()
    {
        return $this->fetch();
    }

    public function adminRoleAdd()
    {
        return $this->fetch();
    }

    public function adminRule()
    {
        return $this->fetch();
    }

    // 停用或启用
    public function adminActive()
    {
        // 0启用状态  1停用状态
        $id = $_POST['id'];
        if ($id == 1){
            return show(434);
        }
        if ($id == session('id')){
            return show(435);
        }
        $active = $_POST['state'];
        if($active == 0){
            db('admin')->where('id',$id)->update(['state'=>0]);
            return show(200);
        }else if ($active == 1){
            db('admin')->where('id',$id)->update(['state'=>1]);
            return show(403);
        }
    }

    public function adminShow()
    {
        $id = input('id');
//        var_dump($id);exit();
        $admin = db('admin')->find($id);
        $this->assign('admin',$admin);
        return $this->fetch();
    }


    public function adminRuleAdd()
    {
        if (request()->isPost())
        {
            $data = [
                'url'   =>  input('url'),
                'tile'   =>  input('title'),
            ];
            if (Db::name('rule')->insert($data))
            {
                return show(200);
            }else{
                return show(403);
            }
        }
        return $this->fetch();
    }

}